#include "vtor_i2c.h"

#ifdef __VTOR_I2C__



// https://blog.csdn.net/xiaoyuanwuhui/article/details/107430279

// 发送IIC起始信号
void VtorI2c_Start(VtorI2c* i2c)
{
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_HIGH); // 拉高信号线
    VtorI2c_Delay(4);
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_HIGH); // 拉高时钟线
    VtorI2c_Delay(4);
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_LOW); // 拉高信号线
    VtorI2c_Delay(4);
}

// 发送IIC停止信号
void VtorI2c_Stop(VtorI2c* i2c)
{
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW); // 拉高时钟线
    VtorI2c_Delay(4);
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_LOW);
    VtorI2c_Delay(4);
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_HIGH);
    VtorI2c_Delay(4);
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_HIGH);
    VtorI2c_Delay(4);
    VtorI2c_Delay(4);
    VtorI2c_Delay(4);
    VtorI2c_Delay(4);
    VtorI2c_Delay(4);
}

// IIC发送ACK信号
void VtorI2c_Ack(VtorI2c* i2c)
{
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
    VtorI2c_Delay(4);
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_LOW);
    VtorI2c_Delay(4);
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_HIGH);
    VtorI2c_Delay(4);
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
    VtorI2c_Delay(4);
}

// IIC不发送ACK信号
void VtorI2c_NAck(VtorI2c* i2c)
{
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
    VtorI2c_Delay(4);
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_HIGH);
    VtorI2c_Delay(4);
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_HIGH);
    VtorI2c_Delay(4);
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
    VtorI2c_Delay(4);
}

// IIC等待ACK信号
unsigned char VtorI2c_WaitAck(VtorI2c* i2c)
{
	uint8_t ack = 0;
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
    VtorI2c_Delay(4);
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_HIGH);

    VtorI2c_SetSdaDir(i2c, GPIO_DIR_INPUT);
    VtorI2c_Delay(4);
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_HIGH);
    VtorI2c_Delay(4);
	ack = VtorI2c_ReadSda(i2c);

	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
    VtorI2c_Delay(4);
    VtorI2c_SetSdaDir(i2c, GPIO_DIR_OUTPUT);
	VtorI2c_Delay(4);
    return ack;
}

// IIC发送一个字节
void VtorI2c_SendByte(VtorI2c* i2c, uint8_t txd)
{
    for(uint8_t i=0; i<8; i++)
    {
    	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
		if(txd & 0x80)
		{
			VtorI2c_WriteSda(i2c, GPIO_LEVEL_HIGH);
		}
		else
		{
			VtorI2c_WriteSda(i2c, GPIO_LEVEL_LOW);
		}
		txd <<= 1;
		VtorI2c_Delay(4);
		VtorI2c_WriteScl(i2c, GPIO_LEVEL_HIGH);
		VtorI2c_Delay(4);
	}
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
	VtorI2c_Delay(4);
}

// IIC读取一个字节
uint8_t	VtorI2c_ReadByte(VtorI2c* i2c)
{
    uint8_t rxd = 0;
    VtorI2c_SetSdaDir(i2c, GPIO_DIR_INPUT);
    for(uint8_t i=0; i<8; i++)
    {
		rxd <<= 1;
		VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
		VtorI2c_Delay(4);
		VtorI2c_WriteScl(i2c, GPIO_LEVEL_HIGH);
		VtorI2c_Delay(4);
		if(VtorI2c_ReadSda(i2c))
		{
			rxd |= 0x01;
		}
    }
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
    VtorI2c_SetSdaDir(i2c, GPIO_DIR_OUTPUT);
	VtorI2c_Delay(4);
    return rxd;
}

// 连续写N个字节
uint8_t VtorI2c_WriteMem(VtorI2c* i2c, uint8_t slaveAddress, uint16_t regAddress, uint8_t regAddrLen , uint8_t* buf, uint16_t len)
{
    VtorI2c_Start(i2c);
    slaveAddress &= 0xfe;
    VtorI2c_SendByte(i2c, slaveAddress);  //发送设备地址+写信号
    if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
    {
    	VtorI2c_Stop(i2c);
    	return VTOR_I2C_STATE_DEV_ADDR_NAK;
    }
    VtorI2c_SendByte(i2c, regAddress);
    if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
    {
    	VtorI2c_Stop(i2c);
    	return VTOR_I2C_STATE_DEV_REG_NAK;
    }
    for(uint16_t i=0; i<len; i++)
    {
    	VtorI2c_SendByte(i2c, buf[i]);
		if(i<len-1)
		{
			if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
			{
				VtorI2c_Stop(i2c);
				return VTOR_I2C_STATE_DEV_DATA_NAKE;
			}
		}
    }
    VtorI2c_NAck(i2c);
    VtorI2c_Stop(i2c);
    return VTOR_I2C_STATE_OK;
}

// 连续读N个字节

uint8_t VtorI2c_ReadMem(VtorI2c* i2c, uint8_t slaveAddress, uint16_t regAddress, uint8_t regAddrLen , uint8_t* buf, uint16_t len)
{
    VtorI2c_Start(i2c);
    slaveAddress &= 0xfe;
    VtorI2c_SendByte(i2c, slaveAddress);  //发送设备地址+写信号
    if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
    {
    	VtorI2c_Stop(i2c);
    	return VTOR_I2C_STATE_DEV_ADDR_NAK;
    }
    VtorI2c_SendByte(i2c, regAddress);
    if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
    {
    	VtorI2c_Stop(i2c);
    	return VTOR_I2C_STATE_DEV_REG_NAK;
    }
    VtorI2c_Start(i2c);
    slaveAddress |= 0x01;
    VtorI2c_SendByte(i2c, slaveAddress); // 读操作
    if(VTOR_I2C_STATE_OK != VtorI2c_WaitAck(i2c))
    {
    	VtorI2c_Stop(i2c);
    	return VTOR_I2C_STATE_DEV_REG_NAK;
    }
    for(uint16_t i=0; i<len; i++)
    {
        buf[i] = VtorI2c_ReadByte(i2c);
		if(i<len-1)
		{
			VtorI2c_Ack(i2c);
		}
    }
    VtorI2c_NAck(i2c);
    VtorI2c_Stop(i2c);
    return VTOR_I2C_STATE_OK;
}

uint8_t VtorI2cMaster_WriteBuffer(VtorI2c* i2c, uint16_t devAddr, uint8_t* txBuf, uint8_t txLen)
{
	uint8_t slaveRxLen = 1;
	uint8_t slaveAckError = 0;
	// 如果从机要发送的数据已经清空了，认为不忙了，可以开始写了
	while(slaveRxLen)
	{
		slaveAckError = VtorI2c_ReadMem(i2c, devAddr,
				VTOR_I2C_REG_SLAVE_RX_LEN, 0x01, &slaveRxLen, 1);
		if(VTOR_I2C_STATE_OK != slaveAckError)
		{
			return 0;
		}
	}
	slaveAckError = VtorI2c_WriteMem(i2c, devAddr,
			VTOR_I2C_REG_SLAVE_RX_BUF, 0x01, txBuf, txLen);
	if(VTOR_I2C_STATE_OK != slaveAckError)
	{
		return 0;
	}
	slaveAckError = VtorI2c_WriteMem(i2c, devAddr,
			VTOR_I2C_REG_SLAVE_RX_LEN, 0x01, &txLen, 1); // 设置写入的数据长度
	if(VTOR_I2C_STATE_OK != slaveAckError)
	{
		return 0;
	}
	return txLen;
}

uint8_t VtorI2cMaster_ReadBuffer(VtorI2c* i2c, uint16_t devAddr, uint8_t* rxBuf, uint8_t rxLen)
{
	uint8_t slaveTxLen= 0;
	uint8_t slaveAckError = 0;
	// 检测从机的 发送缓存 是否存在数据
	
	slaveAckError = VtorI2c_ReadMem(i2c, devAddr,
			VTOR_I2C_REG_SLAVE_TX_LEN, 0x01, (uint8_t*)&slaveTxLen, 1);
	if(VTOR_I2C_STATE_OK != slaveAckError)
	{
		return 0;
	}
	// 取两者较小值
	slaveTxLen = (slaveTxLen < rxLen) ? slaveTxLen : rxLen;
	// 如果没有数据，直接返回
	if(0 == slaveTxLen)
	{
		return 0;
	}
	// 主机接收读取 从机 发送缓存
	slaveAckError = VtorI2c_ReadMem(i2c, devAddr,
		VTOR_I2C_REG_SLAVE_TX_BUF, 0x01, rxBuf, slaveTxLen);
	if(VTOR_I2C_STATE_OK != slaveAckError)
	{
		return 0;
	}
	rxBuf[slaveTxLen] = '\0'; // 截断一下，便于使用字符串
	// 主机反馈 已读取完毕 标志位（向从机txLen写0）
	uint8_t replyLen = 0;
	slaveAckError = VtorI2c_WriteMem(i2c, devAddr,
		VTOR_I2C_REG_SLAVE_TX_LEN, 0x01, (uint8_t*)&replyLen, 1);
	if(VTOR_I2C_STATE_OK != slaveAckError)
	{
		// 如果反馈失败，认为写入失败
		return 0;
	}
	return slaveTxLen;
}

uint8_t VtorI2c_CheckIo(VtorI2c* i2c)
{
	uint8_t ioRet = 0;

	VtorI2c_WriteScl(i2c, GPIO_LEVEL_HIGH);
	VtorI2c_Delay(4);
	if(GPIO_LEVEL_HIGH != VtorI2c_ReadScl(i2c))
	{
		ioRet |= 0x80;
	}
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_LOW);
	VtorI2c_Delay(4);
	if(GPIO_LEVEL_LOW != VtorI2c_ReadScl(i2c))
	{
		ioRet |= 0x40;
	}
    VtorI2c_SetSdaDir(i2c, GPIO_DIR_OUTPUT);
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_HIGH);
	VtorI2c_Delay(4);
	if(GPIO_LEVEL_HIGH != VtorI2c_ReadSda(i2c))
	{
		ioRet |= 0x20;
	}
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_LOW);
	VtorI2c_Delay(4);
	if(GPIO_LEVEL_LOW != VtorI2c_ReadSda(i2c))
	{
		ioRet |= 0x10;
	}
	VtorI2c_WriteSda(i2c, GPIO_LEVEL_HIGH);
	VtorI2c_WriteScl(i2c, GPIO_LEVEL_HIGH);
	return ioRet;
}

uint8_t VtorI2c_CheckDevice(VtorI2c* i2c, uint8_t addr)
{
	uint8_t ret = VTOR_I2C_STATE_ERROR;
	VtorI2c_Start(i2c);
	VtorI2c_SendByte(i2c, addr);
	if(VTOR_I2C_STATE_OK == VtorI2c_WaitAck(i2c))
	{
		ret = VTOR_I2C_STATE_OK;
	}
	VtorI2c_Stop(i2c);
	VtorI2c_Delay(20);
	VtorI2c_Stop(i2c);
	return ret;
}

#endif // __VTOR_I2C__
